/** @namespace app.model.Event */
module.exports = app => {
  const {TEXT, TINYINT} = app.Sequelize;
  const Event = app.model.define('event', {
    message: {type: TEXT, allowNull: false},
    type: {type: TINYINT(1).UNSIGNED, allowNull: false},//0正常事件  1错误事件
  }, {
    updatedAt: false,
    tableName: 'spider_event',
  });

  Event.associate = () => {
    app.model.Platform.hasMany(app.model.Event);
    app.model.Event.belongsTo(app.model.Platform);
  };
  /**
   * 记录
   * @param message
   * @param platform
   * @param type
   * @returns {Promise<void>}
   */
  Event.log = async function(message, platform = null, type = 0) {
    await Event.create(
      {message, platform_id: platform ? platform.id : null, type: 0});
  };

  /**
   * 记录错误
   * @param message
   * @param platform
   * @returns {Promise<void>}
   */
  Event.error = async function(message, platform = null) {
    await Event.create(
      {message, platform_id: platform ? platform.id : null, type: 1});
  };

  return Event;
};